<?php

namespace App\Admin\Controllers;

use App\Admin\Actions\UserAction\RestorePasswordChance;
use App\Models\User;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Show;
use App\Jobs\StormSmsJob;
use Encore\Admin\Layout\Content;
use App\Admin\Tools\UserBatchSmsTool;
use App\Admin\Actions\UserAction\Orders;
use App\Admin\Actions\UserAction\Upgrades;
use App\Admin\Actions\UserAction\TeamOnes;
use App\Admin\Actions\UserAction\TeamTwos;
use App\Admin\Actions\UserAction\VipPurchase;
use App\Admin\Actions\UserAction\DismissMelt;
use App\Admin\Actions\UserAction\UserBalance;
use Encore\Admin\Controllers\AdminController;
use App\Admin\Actions\UserAction\UserGoldBeans;
use App\Admin\Actions\UserAction\SyncUserCache;
use App\Admin\Actions\UserAction\UserGoldCoins;
use App\Admin\Actions\UserAction\DismissPayCode;
use App\Admin\Actions\UserAction\SyncStormCache;
use App\Admin\Actions\UserAction\UserGoldRecords;
use App\Admin\Actions\UserAction\TransferGoldCoins;
use App\Admin\Actions\UserAction\TransferGoldBeans;
use App\Admin\Actions\UserAction\AlipayBindDismiss;
use App\Admin\Actions\UserAction\WechatBindDismiss;
use App\Admin\Actions\UserAction\TransferGoldCashes;
use App\Admin\Actions\UserAction\BankCardBindDismiss;
use App\Admin\Actions\UserAction\TransferGoldSubsidies;
use App\Admin\Actions\UserAction\TransferBalance;
use Illuminate\Support\Facades\Storage;

class CustomersController extends AdminController
{
    protected $title = '客户列表';


    protected function grid()
    {
        $grid = new Grid(new User());
	    $grid->model()->where("grade","customer")->orderByDesc('id');

	    //$this->getTeam($grid);
        
        $grid->column('phone', '手机');
        $grid->column('wx_city', '城市');
        $grid->column('product_id', '商品名称')->display(function () {
            return $this->product ? $this->product->title : '';
        });
        $grid->column('parent_id', '商户名称')->display(function () {
          
            return $this->merchant ? $this->merchant->name : '';
        });

	    $grid->column('created_at', '创建时间')->display(function () {
		    return (string)$this->created_at;
	    });

        $grid->disableRowSelector();
        $grid->disableActions();

        $this->gridTools($grid);
        $this->gridFilter($grid);
        //$this->gridActions($grid);

       // $this->customedGrid($grid);

        return $grid;
    }

    protected function gridTools(Grid $grid)
    {
	    $grid->tools(function (Grid\Tools $tools) {
	    	//$tools->append(new UserBatchSmsTool());
	    });
    }

    protected function gridFilter(Grid $grid)
    {
    	$grid->filter(function (Grid\Filter $filter) {

    		$filter->expand();
    		$filter->column(1/2, function (Grid\Filter $filter) {
			    $filter->endsWith('phone', '手机号');
		    });
	    });
    }

    protected function gridActions(Grid $grid)
    {
	    $grid->actions(function (Grid\Displayers\Actions $actions) {
	    	//$actions->disableDelete();
	    	/*$actions->add(new TransferBalance());*/
	    });
    }

    protected function detail($id)
    {
        $show = new Show(User::findOrFail($id));

       
        $show->field('name', '商家名称');
        $show->field('avatar', '商家门头');
        $show->field('phone', '手机号');
        $show->field('business', '营业执照');
        $show->field('password', '密码');
        $show->field('created_at', '创建时间');
        $show->field('updated_at', '修改时间');

        $show->panel()->tools(function (Show\Tools $tools) {
        	$tools->disableDelete();
        });

        $this->customedDetail($id);

        return $show;
    }

   /* public function smsForm(Content $content)
    {
	    return $content->header('批量发送风暴活动短信')
		    ->body(view('admin.users.batchsms'));
    }*/


    protected function form()
    {
        $form = new Form(new User());

	    $form->text('name', '商家名称')->rules('required');
	    $form->text('phone', '登录手机号')->rules('required');

	    $form->image('avatar', '商家门头')
		    ->uniqueName()
		    ->creationRules('required');

		$form->image('business', '营业执照')
		    ->uniqueName();
		$form->latlong('latitude', 'longitude', 'Position')->height(500);
        $this->formTools($form);
        $this->formSaving($form);

        return $form;
    }

    protected function formTools(Form $form)
    {
    	$form->tools(function (Form\Tools $tools) {
		    $tools->disableDelete();
	    });
    }

    protected function formSaving(Form $form)
    {
    	$form->saving(function (Form $form) {
		    // Passowrd for editing mode
    		if (empty($form->password)) {
		    	$form->password = $form->model()->getOriginal('password');
		    }
    		// Password for creating mode
		    else {
		    	$form->password = bcrypt($form->password);
		    }
	    });
    }
}
